package com.Main.faislamovers;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;

public class MainActivity extends Activity {

	SharedPreferences prefs;

	String NAMESPACE = "http://tempuri.org/";
	String METHODNAME = "login";
	String url = "http://10.0.2.2:53512/Service1.asmx";
	String SopaAction = "http://tempuri.org/login";

	String NAMESPACEFD = "http://tempuri.org/";
	String METHODNAMEFB = "Feedback";
	String urlF = "http://10.0.2.2:53512/Service1.asmx";
	String SopaActionFD = "http://tempuri.org/Feedback";

	EditText edt_user_name, edt_password, edt_full_name, edt_email,
			edt_phn_numb, edt_comment;
	String txt_edt_user_name, txt_edt_password,
			txt_destination, txt_date, txt_edt_full_name, txt_edt_email,
			txt_edt_phn_numb;
	String txt_edt_prn_numb, txt_edt_comment, txt_spinner, txt_spinner_subject;
	Button btn_sign_up, btn_login, btn_find_buses_schudle, btn_feedback;
	Spinner spinner, spinner_subject;
	Spinner spinner_source , spinner_destination;

	
	DatePicker date_time;
	String date_time_get;
	EditText edt_source, edt_destination;
	static String txt_edt_source, txt_edt_dertination;
	static public String result;
	public String[] result_arry;

	private int year;
	private int month;
	private int day;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		prefs = getSharedPreferences(AppConstants.PREFERENCES_NAME, 0);

		edt_user_name = (EditText) findViewById(R.id.txt_user_name_xml);
		edt_password = (EditText) findViewById(R.id.txt_password_xml);

		edt_full_name = (EditText) findViewById(R.id.edt_full_name);
		edt_email = (EditText) findViewById(R.id.edt_email);
		edt_phn_numb = (EditText) findViewById(R.id.edt_phn_numb);
		edt_comment = (EditText) findViewById(R.id.edt_comment);
		
		spinner_source = (Spinner) findViewById(R.id.spinner_source);
		spinner_destination = (Spinner) findViewById(R.id.spinner_destination);

		//edt_source = (EditText) findViewById(R.id.edt_txt_source_schedule);
		//edt_destination = (EditText) findViewById(R.id.edt_txt_destination_schedule);
		date_time = (DatePicker) findViewById(R.id.datePicker_schedule);


		TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
		tabHost.setup();

		spinner = (Spinner) findViewById(R.id.spinner_feedback);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.arry_feedback_type,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);

		spinner_subject = (Spinner) findViewById(R.id.spinner_subject);
		ArrayAdapter<CharSequence> adapter_subject = ArrayAdapter
				.createFromResource(this, R.array.arry_feedback_subject,
						android.R.layout.simple_spinner_item);
		adapter_subject
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner_subject.setAdapter(adapter_subject);
		
		
		ArrayAdapter<CharSequence> adapter_source = ArrayAdapter.createFromResource(
		        this, R.array.arry_list_citys, android.R.layout.simple_spinner_item);
		        adapter_source.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		        spinner_source.setAdapter(adapter_source);
		        
        ArrayAdapter<CharSequence> adapter_destination = ArrayAdapter.createFromResource(
				        this, R.array.arry_list_citys, android.R.layout.simple_spinner_item);
				        adapter_destination.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				        spinner_destination.setAdapter(adapter_destination);
		
		

		TabSpec spec1 = tabHost.newTabSpec("reservation");
		spec1.setIndicator("Reservation",
				getResources().getDrawable(R.drawable.reservation));
		spec1.setContent(R.id.signin);

		TabSpec spec2 = tabHost.newTabSpec("scheduletab");
		spec2.setIndicator("Schedule",
				getResources().getDrawable(R.drawable.schedule));
		spec2.setContent(R.id.Scheduletab);

		TabSpec spec3 = tabHost.newTabSpec("complaintstap");
		spec3.setIndicator("FeedBack",
				getResources().getDrawable(R.drawable.icon1_1));
		spec3.setContent(R.id.complaintstap);

		TabSpec spec4 = tabHost.newTabSpec("contactstab");
		spec4.setIndicator("Contacts Us",
				getResources().getDrawable(R.drawable.icon_contabs));
		spec4.setContent(R.id.Contactstab);

		tabHost.addTab(spec1);
		tabHost.addTab(spec2);
		tabHost.addTab(spec3);
		tabHost.addTab(spec4);

		btn_sign_up = (Button) findViewById(R.id.btn_sginup_xml);
		btn_login = (Button) findViewById(R.id.btn_findbuses_xml);
		btn_feedback = (Button) findViewById(R.id.btn_submit_feedback);
		
		btn_sign_up.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v)
			{
				
				startActivity(new Intent(MainActivity.this , Signup.class));
			}
		});
		
		

		btn_feedback.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			
				try {
					txt_edt_full_name = edt_full_name.getText().toString();
					txt_edt_email = edt_email.getText().toString();
					txt_edt_phn_numb = edt_phn_numb.getText().toString();
					txt_edt_comment = edt_comment.getText().toString();
					txt_spinner = spinner.getSelectedItem().toString();
					txt_spinner_subject = spinner_subject.getSelectedItem()
							.toString();

					setcustMsisdnFB();
			
					
				} catch (Exception e) {

				}

			}
		});

		btn_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (v.getId() == R.id.btn_findbuses_xml) {
					

					try {
						txt_edt_user_name = edt_user_name.getText().toString();
						txt_edt_password = edt_password.getText().toString();
						
						if (txt_edt_user_name.equals("")|| txt_edt_user_name.equals(null)) 
						{
							Toast.makeText(getBaseContext(),"Sorry User Login is required!",Toast.LENGTH_LONG).show();
						} 
						
						else if (txt_edt_password.equals("")|| txt_edt_password.equals(null)) {
							Toast.makeText(getBaseContext(),"Sorry User Password is required!", Toast.LENGTH_LONG).show();
						} 
						
						else 
						{
								if (setcustMsisdn() == true) {

							startActivity(new Intent(MainActivity.this,
									Login.class));

						}

						else {

							Toast.makeText(MainActivity.this, "Try Again",
									Toast.LENGTH_LONG).show();
						}
						}
						
					}

					catch (Exception e) {

					}
				}
			}
		});
		
		
		Button btn_find_buses = (Button) findViewById(R.id.btn_find_buses_schedule);
		btn_find_buses.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					
					// txt_edt_source = spinner_source.getSelectedItem().toString();
				//	txt_edt_dertination = spinner_destination.getSelectedItem().toString();

					txt_edt_source = spinner_source.getSelectedItem().toString();
					txt_edt_dertination = spinner_destination.getSelectedItem().toString();
					if (txt_edt_dertination.equals("")
							|| txt_edt_dertination.equals(null)) {
						Toast.makeText(getBaseContext(),
								"Sorry Destination is required!",
								Toast.LENGTH_LONG).show();
					} else if (txt_edt_source.equals("")
							|| txt_edt_source.equals(null)) {
						Toast.makeText(getBaseContext(),
								"Sorry Source is required!", Toast.LENGTH_LONG)
								.show();
					} else {
						SharedPreferences pref = getSharedPreferences(
								AppConstants.PREFERENCES_NAME, 0);
						year = date_time.getYear();
						month = date_time.getMonth();
						day = date_time.getDayOfMonth();
						SimpleDateFormat sdf = new SimpleDateFormat(
								"MM/dd/yyyy");

						Date date = new Date();
						date.setDate(day);
						date.setMonth(month);
						date.setYear(year - 1900);
						String dateString = sdf.format(date);

						Editor editor = pref.edit();
						editor.putString(AppConstants.SOURCE, txt_edt_source);
						editor.putString(AppConstants.DESTINATION,
								txt_edt_dertination);
						editor.putString(AppConstants.DATE, dateString);
						editor.commit();

					
						Intent intent = new Intent();
						intent.putExtra(AppConstants.SOURCE, txt_edt_source);
						intent.putExtra(AppConstants.DESTINATION,
								txt_edt_dertination);
						intent.putExtra(AppConstants.DATE, dateString);

						intent.setClass(MainActivity.this, ViewBusesSchedule.class);
						startActivity(intent);
						finish();
					}

				}

				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;

	}

	public boolean setcustMsisdn() throws IOException, XmlPullParserException {

		SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
		request.addProperty("user_name", txt_edt_user_name);
		request.addProperty("password", txt_edt_password);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		MarshalBase64 m = new MarshalBase64();
		m.register(envelope);
		envelope.encodingStyle = SoapEnvelope.ENC;
		// send request
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
		androidHttpTransport.call(SopaAction, envelope);

		String result = envelope.getResponse().toString();

		Editor editior = prefs.edit();
		editior.putString(AppConstants.USER_ID, result);
		editior.commit();

		if (result.equals("false")) {
			Toast.makeText(MainActivity.this, "Login Faild", Toast.LENGTH_LONG)
					.show();
			return false;
		} 
		else {
			Toast.makeText(MainActivity.this, "Login Success",
					Toast.LENGTH_LONG).show();
			return true;
		}

	}

	public boolean setcustMsisdnFB() throws IOException, XmlPullParserException {

		SoapObject request = new SoapObject(NAMESPACEFD, METHODNAMEFB);

		request.addProperty("feedback_type", txt_spinner_subject);
		request.addProperty("subject", txt_spinner);
		request.addProperty("comment", txt_edt_comment);
		request.addProperty("name", txt_edt_full_name);
		request.addProperty("email_id", txt_edt_email);
		request.addProperty("phn_number", txt_edt_phn_numb);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		MarshalBase64 m = new MarshalBase64();
		m.register(envelope);
		envelope.encodingStyle = SoapEnvelope.ENC;
		// send request
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(urlF);
		androidHttpTransport.call(SopaActionFD, envelope);
		// get response
		KvmSerializable ks = (KvmSerializable) envelope.bodyIn;
		String result = ks.getProperty(0).toString();

		if (result.equals("Successs")) {
			Toast.makeText(MainActivity.this, "Feedback Success",
					Toast.LENGTH_LONG).show();
			return true;
		} else {
			Toast.makeText(MainActivity.this, "FeedBack Failed",
					Toast.LENGTH_LONG).show();
			return false;
		}

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.item_about:

			Intent i = new Intent("com.Main.faislamovers.ABOUTUS");
			startActivity(i);

			break;

		case R.id.item_help:

			Intent h = new Intent("com.Main.faislamovers.HELP");
			startActivity(h);

			break;


		case R.id.item_feedback:
			Intent feedbackI = new Intent("com.Main.faislamovers.MYBOOKING");
			startActivity(feedbackI);

			break;

		case R.id.item_exit:
			finish();
			break;

		case R.id.item_terminls:
			Intent termainlsI = new Intent("com.Main.faislamovers.TERMAINLS");
			startActivity(termainlsI);
			break;

		}
		return false;
	}

}
